home *** CD-ROM | disk | FTP | other *** search
/ PC World Komputer 2001 December / pcwk12201b.iso / Wersje pelne i specjalne / Winamp 2.77 i 3.0beta / wasabi-sdk_beta1.exe / studio / common / nsGUID.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2001-10-08  |  3.9 KB  |  125 lines

  1. /*
  2.  
  3.   Nullsoft WASABI Source File License
  4.  
  5.   Copyright 1999-2001 Nullsoft, Inc.
  6.  
  7.     This software is provided 'as-is', without any express or implied
  8.     warranty.  In no event will the authors be held liable for any damages
  9.     arising from the use of this software.
  10.  
  11.     Permission is granted to anyone to use this software for any purpose,
  12.     including commercial applications, and to alter it and redistribute it
  13.     freely, subject to the following restrictions:
  14.  
  15.     1. The origin of this software must not be misrepresented; you must not
  16.        claim that you wrote the original software. If you use this software
  17.        in a product, an acknowledgment in the product documentation would be
  18.        appreciated but is not required.
  19.     2. Altered source versions must be plainly marked as such, and must not be
  20.        misrepresented as being the original software.
  21.     3. This notice may not be removed or altered from any source distribution.
  22.  
  23.  
  24.   Brennan Underwood
  25.   brennan@nullsoft.com
  26.  
  27. */
  28.  
  29. #include "nsGUID.h"
  30.  
  31. #include <stdio.h>
  32.  
  33. // Remember what a GUID is, goofball?
  34. #include "GUID.h"
  35.  
  36. //
  37. //    Yes I'm a lazy twirp.  I couldn't find any useful textparsing
  38. //    stuff, but I know there HAS to be some, somewhere.
  39. //
  40. //    So, instead, for now, I'm just using printf and scanf
  41. //    to read and write GUIDs as human readable strings.
  42. //
  43. char *    nsGUID::toChar( const GUID &guid, char *target ) {
  44.     // Better hope your array has enough room, bucko!
  45.  
  46.     // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  47.     sprintf( target, "{%08X-%04X-%04X-%02X%02X-%02X%02X%02X%02X%02X%02X}",
  48.         (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  49.         (int)guid.Data4[0], (int)guid.Data4[1], 
  50.         (int)guid.Data4[2], (int)guid.Data4[3], 
  51.         (int)guid.Data4[4], (int)guid.Data4[5], 
  52.         (int)guid.Data4[6], (int)guid.Data4[7] );
  53.  
  54.     return target;
  55. }
  56.  
  57. GUID        nsGUID::fromChar( const char *source ) {
  58.     GUID guid;
  59.     int Data1, Data2, Data3;
  60.     int Data4[8];
  61.  
  62.     // {1B3CA60C-DA98-4826-B4A9-D79748A5FD73}
  63.     sscanf( source, " { %08x - %04x - %04x - %02x%02x - %02x%02x%02x%02x%02x%02x } ",
  64.         &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1, 
  65.         Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  66.  
  67.     // Cross assign all the values.  Yes, this is slow too.
  68.     guid.Data1 = Data1;
  69.     guid.Data2 = Data2;
  70.     guid.Data3 = Data3;
  71.     guid.Data4[0] = Data4[0];
  72.     guid.Data4[1] = Data4[1];
  73.     guid.Data4[2] = Data4[2];
  74.     guid.Data4[3] = Data4[3];
  75.     guid.Data4[4] = Data4[4];
  76.     guid.Data4[5] = Data4[5];
  77.     guid.Data4[6] = Data4[6];
  78.     guid.Data4[7] = Data4[7];
  79.  
  80.     // and spit it out.
  81.     return guid;
  82. }
  83.  
  84. char *    nsGUID::toCode( const GUID &guid, char *target ) {
  85.     // Better hope your array has enough room, bucko!
  86.  
  87.     //{ 0x1b3ca60c, 0xda98, 0x4826, { 0xb4, 0xa9, 0xd7, 0x97, 0x48, 0xa5, 0xfd, 0x73 } };
  88.     sprintf( target, "{ 0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x } };",
  89.         (int)guid.Data1, (int)guid.Data2, (int)guid.Data3,
  90.         (int)guid.Data4[0], (int)guid.Data4[1], 
  91.         (int)guid.Data4[2], (int)guid.Data4[3], 
  92.         (int)guid.Data4[4], (int)guid.Data4[5], 
  93.         (int)guid.Data4[6], (int)guid.Data4[7] );
  94.  
  95.     return target;
  96. }
  97.  
  98. GUID        nsGUID::fromCode( const char *source ) {
  99.  
  100.     GUID guid;
  101.     int Data1, Data2, Data3;
  102.     int Data4[8];
  103.  
  104.     //{ 0x1b3ca60c, 0xda98, 0x4826, { 0xb4, 0xa9, 0xd7, 0x97, 0x48, 0xa5, 0xfd, 0x73 } };
  105.     sscanf( source, " { 0x%08x, 0x%04x, 0x%04x, { 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x, 0x%02x } } ; ",
  106.         &Data1, &Data2, &Data3, Data4 + 0, Data4 + 1, 
  107.         Data4 + 2, Data4 + 3, Data4 + 4, Data4 + 5, Data4 + 6, Data4 + 7 );
  108.  
  109.     // Cross assign all the values.  Yes, this is slow too.
  110.     guid.Data1 = Data1;
  111.     guid.Data2 = Data2;
  112.     guid.Data3 = Data3;
  113.     guid.Data4[0] = Data4[0];
  114.     guid.Data4[1] = Data4[1];
  115.     guid.Data4[2] = Data4[2];
  116.     guid.Data4[3] = Data4[3];
  117.     guid.Data4[4] = Data4[4];
  118.     guid.Data4[5] = Data4[5];
  119.     guid.Data4[6] = Data4[6];
  120.     guid.Data4[7] = Data4[7];
  121.  
  122.     // and spit it out.
  123.     return guid;
  124. }
  125.